// pages/real/components/detailFooter/index.js
const app = getApp()
const utils = require('../../../../utils/util.js')
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    list:{
      type:Object,
      value:"default value"
    },
    userInfo:{
      type: Object,
      value:"default value"
    },
    phone:{
      type:String,
      value:"default value"
    },
    background:{
      type:String,
      value:"#ffffff"
    },
    icons:{
      type:String,
      value:"#BBBBBB"
    },
    color:{
      type:String,
      value:"#888888"
    },
    routes:{
      type:String,
      value:'default value'
    },
    types:{
      type:String,
      value:''
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    isIphoneX: app.globalData.isIphoneX,
  },
  /**
   * 组件的方法列表
   */
  methods: {
    handleAddLike(){
      // console.log(this.properties)
      const userInfo=wx.getStorageSync('userInfo')
      if(userInfo) {
        const unionid=wx.getStorageSync("unionid")
        const list=this.properties.list
        const str={
          id:list.id,
          createBy:unionid
        }
        if(list.isLike==0){
          utils.$http(app.globalData.api.getRealAddLike, str, 'POST', true, 'JSON').then(res => {
            wx.showToast({
              title: '点赞成功',
              type: 'success'
            })
            app.getPrePage().initDetailList()
        })
        }else{
          utils.$http(app.globalData.api.getRealCancelLike, str, 'POST', true, 'JSON').then(res => {
            wx.showToast({
              title: '取消点赞成功',
              type: 'success'
            })
            app.getPrePage().initDetailList()
        })
        }
      }
      else {
        this.toLoginPage()
      }
    },
    handleAddCollect() {
      if(this.data.userInfo) {
        const userInfo=wx.getStorageSync('userInfo')
        const unionid=wx.getStorageSync("unionid")
        const list=this.properties.list
        const str={
          createBy:unionid,
          id:list.id
        }
        if(list.isKeep==0){
          utils.$http(app.globalData.api.realityContributeAddKeep, str, 'POST', true, 'JSON').then(res => {
          wx.showToast({
            title: '收藏成功',
            type: 'success'
          })
          app.getPrePage().initDetailList()
        })
      }else{
        utils.$http(app.globalData.api.realityContributeUnKeep, str, 'POST', true, 'JSON').then(res => {
          wx.showToast({
            title: '取消收藏成功',
            type: 'success'
          })
          app.getPrePage().initDetailList()
        })
      }
    } else {
      this.toLoginPage()
    }
  },
  // 跳登录页
  toLoginPage() {
    let message = ''
    if(!this.data.userInfo) {
      message = '未登陆，是否跳到登陆页登陆'
    } 
    // const { id ,routes} = this.data
    wx.showModal({
      title: '提示',
      content: message,
      success:function(res) {
        if(res.confirm){
          wx.navigateTo({
            url: '/pages/mine/register/index'
          });
        }else if (res.cancel) {
          wx.switchTab({
            url: '/pages/real/homepage/index'
          });
        }
      }
    })
  },
    // 分享好友
    handleShare() {
    //  console.log('handleShare')
     this.setData({
       clickShareFlag: true
     })
      // const res = await this.getShare()
      // const code = res && res.data // 用于分享卡片找对应的方案id 用于记录分享数量
     
      const imageURl =  utils.filterURL(this.data.list.realityPic)
      const url = '/pages/share/index?id=' + this.data.list.id + '&code=' + '123' + '&shareName=' + this.data.list.realityName + '&url=' + imageURl + "&source=ads" + '&routes='+ this.data.routes +'&real=real'
      const that = this
      wx.navigateTo({
         url: url,
         complete: function(e) {
          that.setData({
            clickShareFlag: false
          })
         }
       })
    }
  },
})
